package com.hp.bon.sgw.start;

import static com.hp.ocs.diameter.ProtocolConstants.DIAMETER_APPLICATION_CREDIT_CONTROL;
import static com.hp.ocs.diameter.ProtocolConstants.DI_SESSION_ID;

import java.net.URI;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.hp.ocs.diameter.DiameterUtils;
import com.hp.ocs.diameter.ProtocolConstants;
import com.hp.ocs.diameter.message.AVP;
import com.hp.ocs.diameter.message.AVP_Grouped;
import com.hp.ocs.diameter.message.AVP_UTF8String;
import com.hp.ocs.diameter.message.AVP_Unsigned32;
import com.hp.ocs.diameter.message.DictionaryManager;
import com.hp.ocs.diameter.message.InvalidAVPLengthException;
import com.hp.ocs.diameter.message.Message;
import com.hp.ocs.diameter.node.Capability;
import com.hp.ocs.diameter.node.InvalidSettingException;
import com.hp.ocs.diameter.node.NodeSettings;
import com.hp.ocs.diameter.node.Peer;
import com.hp.ocs.diameter.node.PeerFactory;
import com.hp.ocs.diameter.node.SimpleSyncClient;

public class DCCTestClienter
{
	SimpleSyncClient _diameter;
	NodeSettings _setting;
	public static DictionaryManager _dic = DictionaryManager.getInstance();
	public static void main(String[] args) throws Exception
    {
		DCCTestClienter client=new DCCTestClienter();
		client.start();
		client.sendAndReceive();
		client.stop();
    }

	private NodeSettings configure() throws InvalidSettingException
	{
		Capability capability = new Capability();
		capability.addAuthApp(0);
		capability.addAuthApp(4);
		capability.addAcctApp(0);
		capability.addAcctApp(4);
		_setting = new NodeSettings(
				"dccNE@001.ctnbc-bon.net",
				"001.ctnbc-bon.net",
				81000,
				capability,
				null, 0,
				"Simple Client for Diameter Framework", 0x01000000);
		return _setting;
	}

	private void sendAndReceive()
    {
		
		Message req=makeCCR();
		System.out.println("Request:"+req.getDictionary());
		Message resp=_diameter.sendRequest(req);
		if(resp!=null) System.out.println("Response:"+resp.getDictionary());
		try
        {
			AVP avp = resp.find(268);//Result-Code AVP code
	        int resultCode = new AVP_Unsigned32(avp).queryValue();
	        System.out.println("Result_Code = "+resultCode);
        }
        catch (InvalidAVPLengthException e)
        {
	        e.printStackTrace();
        }
    }

	private void stop()
    {
	  if(_diameter!=null) _diameter.stop();
    }

	private void start() throws Exception
    {
		_setting=configure();
		//String hostUri = "aaa://127.0.0.1:6688";
		//wuzh
		String hostUri = "aaa://127.0.0.1:3866";
		URI uri = new URI(hostUri);
		Peer peer = PeerFactory.getInstance().getPeerServer(hostUri, null,uri);
		List<Peer> peers = new ArrayList<Peer>();
		peers.add(peer);
		_diameter=new SimpleSyncClient(_setting,peers);
		_diameter.start();
		while(!_diameter.isPeerReady(peer))
		{
			System.out.print(".");
			Thread.sleep(1000);
		}
    }
	
	
	
	private Message makeCCR()
	{
		Message message = new Message();
		message.header().setRequest(true);
		message.header().setProxiable(true);
		message.header().setApplicationID(ProtocolConstants.DIAMETER_APPLICATION_CREDIT_CONTROL);
		message.header().setCommandCode(272);

		String sessionID = DiameterUtils.makeSessionID(_setting.hostId());
		message.add(new AVP_UTF8String(DI_SESSION_ID, sessionID));
		_diameter.addOurHostAndRealm(message);//Origin-Host,Origin-Realm
		
		message.add(_dic.makeAVP("Destination-Host", "aaa://in.test.com:3868"));
		message.add(_dic.makeAVP("Destination-Realm", "test.com"));
		message.add(_dic.makeAVP("Auth-Application-Id", DIAMETER_APPLICATION_CREDIT_CONTROL));
		message.add(_dic.makeAVP("Service-Context-Id", "version1.p2psms@chinatelecom.com"));
		message.add(_dic.makeAVP("CC-Request-Type", 4));
		message.add(_dic.makeAVP("CC-Request-Number", 0));
		message.add(_dic.makeAVP("Event-Timestamp", new Date()));
		AVP[] avps=new AVP[]{
				_dic.makeAVP(DIAMETER_APPLICATION_CREDIT_CONTROL, "Subscription-Id-Type", 0),
				_dic.makeAVP(DIAMETER_APPLICATION_CREDIT_CONTROL, "Subscription-Id-Data", "123456")
				};
		message.add(_dic.makeAVP(DIAMETER_APPLICATION_CREDIT_CONTROL, "Subscription-Id", avps));
		AVP[] avpForwards=new AVP[]{
				new AVP_UTF8String(80048, 81000,"Self-Host"),
				new AVP_UTF8String(80049, 81000,"Downstream-Hos"),
				new AVP_UTF8String(80049, 81000,"Response-Time")};
		message.add(new AVP_Grouped(80047,81000,avpForwards));
		//...
		return message;
	}
	
}
